package com.fdb.efp.flow.api.rest;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.flow.service.facade.BizFlowNodeService;
import com.fdb.efp.flow.service.facade.BizFlowProcessService;
import com.fdb.efp.flow.service.vo.BizFlowNodeVO;
import com.fdb.efp.flow.service.vo.BizFlowProcessVO;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.log.LogManager;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizFlowProcessController extends AbstractController  {

	@Autowired
	@Qualifier("bizFlowProcessService")
	private BizFlowProcessService bizFlowProcessService;

	@Autowired
	private BizFlowNodeService bizFlowNodeService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/flow/processs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowProcessVO>> queryBizFlowProcessAll(
			@RequestBody BizFlowProcessVO vo) {		
		
		LogManager.info(vo.getBizFlowId(), bizFlowProcessService, "queryAll");
		
		//List<BizFlowProcessVO> outputVo = this.executeQueryList("queryAll", bizFlowProcessService, new Object[]{vo});

		List<BizFlowProcessVO> outputVo = null;
		
		// 根据用户角色获取用户权限方案
		//Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		//DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		//setUserInfoToVO(vo);
		DataRuleType dataRuleType = DataRuleType.getDataRuleType("01");
		switch (dataRuleType) {
		case Owner:
			outputVo = bizFlowProcessService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizFlowProcessService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizFlowProcessService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizFlowProcessService.queryAllOwner(vo);
			break;
		}

		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/flow/process/{bizFlowId}{bizNodeId}/", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizFlowProcessVO> queryByPk(@PathVariable("bizFlowId") String bizFlowId,@PathVariable("bizNodeId") String bizNodeId) {		
		BizFlowProcessVO inVo = new BizFlowProcessVO();
		inVo.setBizFlowId(bizFlowId);
		inVo.setBizNodeId(bizNodeId);
		BizFlowProcessVO outputVo = bizFlowProcessService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * @param bizFlowProcess
	 * @return
	 */
	@RequestMapping(value="/biz/flow/process", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizFlowProcessVO inBizFlowProcessVo) {		
		int outputVo = bizFlowProcessService.deleteByPk(inBizFlowProcessVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * @param inBizFlowProcessVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/process", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizFlowProcessVO inBizFlowProcessVo) {		
		int outputVo = bizFlowProcessService.updateByPk(inBizFlowProcessVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * @param inBizFlowProcessVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/process", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizFlowProcess(@RequestBody BizFlowProcessVO inBizFlowProcessVo) {
		int outputVo = bizFlowProcessService.insertBizFlowProcess(inBizFlowProcessVo);
		return getResponseData(outputVo);

	}

	/**
	 * 
	 * @param inBizFlowProcessVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/nodeForProcess", method=RequestMethod.GET)
	public @ResponseBody ResponseData<List<BizFlowNodeVO>> queryBizFlowForProcess(@RequestParam("bizFlowId") String bizFlowId,@RequestParam("bizEventId") String bizEventId) {
		//查询流转配置节点
		BizFlowNodeVO bizFlowNodeVO = new BizFlowNodeVO();
		bizFlowNodeVO.setBizFlowId(bizFlowId);
		List<BizFlowNodeVO> nodeList = bizFlowNodeService.queryAllByFlowId(bizFlowNodeVO);

		if(nodeList != null && nodeList.size() > 0) {
			//查询事件下节点
			BizFlowProcessVO inBizFlowProcessVo = new BizFlowProcessVO();
			inBizFlowProcessVo.setBizFlowId(bizFlowId);
			inBizFlowProcessVo.setBizEventId(bizEventId);
			List<BizFlowProcessVO> processList = bizFlowProcessService.queryAllByEventIdAndFlowId(inBizFlowProcessVo);
			Set<String> processSet = new HashSet<>();
			if(processList != null && processList.size() > 0) {
				for(int i=0; i<processList.size(); i++) {
					processSet.add(processList.get(i).getBizNodeId());
				}
			}

			//标志下节点是否被事件引用
			for(int i=0; i<nodeList.size(); i++) {
				if(StringUtil.isNotEmpty(nodeList.get(i).getBizNodeId())
						&& processSet.contains(nodeList.get(i).getBizNodeId())) {
					nodeList.get(i).setEventNode(true);
				}
			}
		}

		return getResponseData(nodeList);
	}
}
